﻿using System.Collections.Generic;
using Loki.IoC;
using Loki.Utils;
using Luna.Data.SqlServer;
using NUnit.Framework;

namespace Luna.Data.Test
{
    [TestFixture("LocalSqlServer")]
    public abstract class DataTest : AssertionHelper
    {
        public DataTest(string providerType)
        {
            if (providerType == "LocalSqlServer")
            {
                Install(DatabaseInstaller.LocalSql);
            }

            Install(DataInstaller.All);

            DataManager = new Manager();

            Provider = providerType;
        }

        protected string Provider { get; private set; }

        #region IoC

        private const string TestContextName = "TestContext";

        protected IObjectCreator Context
        {
            get { return internalContext; }
        }

        private IObjectContext internalContext;

        private List<IContextInstaller> installerList = new List<IContextInstaller>();

        protected void Install(params IContextInstaller[] installers)
        {
            installerList.AddRange(installers);
        }

        #endregion IoC

        [TestFixtureSetUp]
        public virtual void SetUp()
        {
            internalContext = Toolkit.IoC.CreateContext(TestContextName);
            foreach (var installer in installerList)
            {
                installer.Install(internalContext);
            }
        }

        [TestFixtureTearDown]
        public virtual void TearDown()
        {
            if (internalContext != null)
            {
                Toolkit.IoC.DropContext(internalContext);
            }
        }

        #region Random values

        protected Manager DataManager
        {
            get;
            private set;
        }

        #endregion Random values
    }
}